home *** CD-ROM | disk | FTP | other *** search
/ El Mac 9 / El Mac 9.iso / Shareware / Applications / MathPad 2.4 / Examples / Chebyshev next >
Encoding:
Text File  |  1996-04-06  |  1.4 KB  |  52 lines  |  [TEXT/MPad]

  1. -- An arbitrary function can be approximated by a weighted sum of Chebyshev polynomials.
  2. -- A Chebyshev polynomial of degree n is defined as:
  3. -- T(x)[n] = 1 when n=0,
  4. --           x when n=1,
  5. --           2*x*T(x)[n-1] - T(x)[n-2]
  6. -- or the explicit formula:
  7.  
  8. T(x)[n] = cos(n*acos(x))
  9.  
  10. Xmin=-1; Xmax=1; Title="Chebyshev polynomials"
  11. plot T(X)[0:6]  -- show the first few polynomials
  12.  
  13. -- The weighting coefficients can be calculated using the values of the arbitrary function at the zeros of T(x). T(x)[N] has N zeros between -1 and +1 located at:
  14.  
  15. xz[k] = cos(pi*Radians*(k-.5)/N) dim[N]
  16.  
  17. -- the N coefficients:
  18. c[j] = (2/N)*sum(f(xz[k])*T(xz[k])[j-1],k,1,N) dim[N]
  19. cc:=c:;  -- save the coefficients
  20.  
  21. -- the approximation formula:
  22. approx(cc,x) = sum(cc[k]*T(x)[k-1],k,1,count(cc)) - cc[1]/2
  23.  
  24. -- Try an example function. Smooth functions are easy. Try something a bit tougher.
  25.  
  26. f(x) = x when x<-.5, -.5 when x<.5, -x
  27.  
  28. N=15;  -- number of coefficients to use
  29.  
  30. newaxis
  31. plot f(X)
  32. plot {xz,f(xz)}
  33. plot approx(cc,X)
  34.  
  35. -- Once the coefficients are known they can be used to derive coefficients to approximate the integral.
  36.  
  37. ci[i] = (cc[i-1]-cc[i+1])/(2*(i-1)) when i>1,
  38.           ci0 dim[N-1]
  39. ci0 = 0   -- arbitrary constant of integration
  40.  
  41. newaxis 
  42. plot approx(ci,X)
  43.  
  44. -- They can also be used to find coefficients to approximate the the derivative.
  45.  
  46. cd:=0[1:N]:
  47. cd[N-1]:=2*(N-1)*cc[N]:
  48. j:=N-2:
  49. (cd[j]:=cd[j+2] + 2*j*cc[j+1], j:=j-1) while j>0:
  50.  
  51. plot approx(cd,X)
  52.